<?php

namespace wxpay;
/*
 * 联速lspay
 */
use think\Log;
class lspay
{
    protected $appid;
    protected $mch_id;
    protected $key;
    protected $openid;
    public function __construct($openid, $mch_id, $key, $app_id, $name, $ooid, $price,$notifyurl)
    {
        $this->openid = $openid;
        $this->mch_id = $mch_id;
        $this->key = $key;
        $this->app_id = $app_id;
        $this->name = $name;
        $this->ooid = $ooid;
        $this->price = $price;
        $this->notifyurl = $notifyurl;
    }
    public function pay()
    {
        //统一下单接口
        $return = $this->weixinapp();
        return $return;
    }
    //统一下单接口
    private function unifiedorder()
    {
        
        $parameters = array(
            'cusid' =>$this->mch_id,
            'appid' =>$this->app_id, 
            'version' =>"12", 
            'trxamt' => "".$this->price * 100,
            'reqsn' => $this->ooid,
            'notify_url' => $this->notifyurl, //通知地址 
            'body' => $this->name, //商品描述
            'paytype' =>"W06",
            'signtype'=>"RSA",
            "isdirectpay"=>"1",
            'randomstr' =>$this->createNoncestr(16), 
        );
        //统一下单签名
         ksort($parameters);
        $parameters['sign'] = $this->getSign($parameters);

        return $parameters;
    }
    //获取13位时间戳
    protected function getTime13(){
        list($t1, $t2) = explode(' ', microtime());
        return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
    }

    private static function postXmlCurl($reqdata, $url, $second = 30)
    {
        // halt($reqdata);
        $ch = curl_init();
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //严格校验
        //设置header
//        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //post提交方式
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $reqdata);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
        curl_setopt($ch, CURLOPT_TIMEOUT, 40);
        set_time_limit(0);
        log::write(["msg"=>"联速请求参数","data"=>$reqdata],"log");
// halt($reqdata);
        //运行curl
        $data = curl_exec($ch);
        // halt($data);
        //返回结果
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            curl_close($ch);
            echo ("curl出错，错误码:$error");
        }
    }

    //微信小程序接口
    private function weixinapp()
    {
        //统一下单接口
        $unifiedorder = $this->unifiedorder();
        
        $unifiedorder['appId']=1;
        
        return $unifiedorder;
    }
    //作用：生成签名
    private function getSign($array)
    {

       $buff = "";
		foreach ($array as $k => $v)
		{
			if($v != "" && !is_array($v)){
				$buff .= $k . "=" . $v . "&";
			}
		}
		
		$buff = trim($buff, "&");
		
		$private_key=$this->key;
    $private_key = chunk_split($private_key , 64, "\n");
    $key = "-----BEGIN RSA PRIVATE KEY-----\n".wordwrap($private_key)."-----END RSA PRIVATE KEY-----";

	 if(openssl_sign($buff, $signature, $key)){
	//		echo 'sign success';
		}else{
			echo 'sign fail';
		} 
$sign = base64_encode($signature);//加密后的内容通常含有特殊字符，需要编码转换下，在网络间通过url传输时要注意base64编码是否是url安全的

		return $sign;
	
    }
    //作用：产生随机字符串，不长于32位
    private function createNoncestr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
}
